package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.UserDTO;
import net.wanho.dto.UserModifyPwdDTO;
import net.wanho.dto.UserRoleDTO;
import net.wanho.entity.UserInfo;
import net.wanho.service.AuthInfoService;
import net.wanho.service.RoleAuthService;
import net.wanho.service.RoleService;
import net.wanho.service.UserInfoService;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private final AuthInfoService authInfoService;
    private final UserInfoService userInfoService;
    private final RoleService roleService;
    private final RoleAuthService roleAuthService;
    /**
     *  获取用户权限列表
     */
    @GetMapping("/auth-list")
    public AjaxResult authList() {
        return AjaxResult.success(authInfoService.getAuthList());
    }

    /**
     *  获取用户列表
     */
    @GetMapping("/user-list")
    public AjaxResult userList( UserDTO userDTO,
                               @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(required = false, defaultValue = "10")Integer pageSize) {
        return AjaxResult.success(userInfoService.getUserListPage(userDTO, pageNum, pageSize));
    }

    /**
     *  添加用户
     */
    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestBody UserDTO userDTO) {
        System.out.println("userDTO:" + userDTO);
        userInfoService.saveUser(userDTO);
        return AjaxResult.success(null, "用户添加成功！");
    }

    /**
     *  修改用户状态
     */
    @PutMapping("/updateState")
    public AjaxResult updateState(@RequestBody UserInfo userInfo) {
        userInfoService.updateById(userInfo);
        return AjaxResult.success(null, "用户状态修改成功！");
    }

    /**
     *  根据id查询用户
     */
    @GetMapping("/{id}")
    public AjaxResult getUserById(@PathVariable Integer id) {
        return AjaxResult.success(userInfoService.getById(id));
    }

    /**
     *  编辑用户信息
     */
    @PutMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody UserInfo userInfo) {
        userInfoService.updateById(userInfo);
        return AjaxResult.success(null, "用户修改成功！");
    }

    /**
     *  删除用户
     */
    @DeleteMapping("/deleteUser/{id}")
    public AjaxResult deleteUser(@PathVariable Integer id) {
        userInfoService.updateUserById(id);
        return AjaxResult.success(null, "用户删除成功！");
    }

    /**
     *  批量删除用户
     */
    @DeleteMapping("/deleteUserList")
    public AjaxResult deleteUserList(@RequestBody List<Integer> ids) {
        userInfoService.removeUserBatchByIds(ids);
        return AjaxResult.success(null, "用户删除成功！");
    }

    /**
     *  重置密码
     *
     */
    @PutMapping("/updatePwd/{id}")
    public AjaxResult updatePwd(@PathVariable Integer id) {
        userInfoService.updatePwdById(id);
        return AjaxResult.success(null, "密码重置成功！");
    }

    /**
     *  根据用火狐Id查询角色
     */
    @GetMapping("/user-role-list/{id}")
    public AjaxResult userRoleList(@PathVariable Integer id) {
        return AjaxResult.success(roleService.getUserRoleListByUserId(id));
    }

    /**
     *  分配角色
     */
    @PutMapping("/assignRole")
    public AjaxResult assignRole(@RequestBody UserRoleDTO userRoleDTO) {
        userInfoService.assignRole(userRoleDTO);
        return AjaxResult.success(null, "分配角色成功！");
    }

    /**
     *  根据用户Id查询权限Id
     */
    @GetMapping("/user-auth")
    public AjaxResult userAuth(Integer userId) {
        return AjaxResult.success(roleAuthService.getUserAuthIdList(userId));
    }

    /**
     *  导出用户信息
     */
    @GetMapping("/exportTable")
    public AjaxResult exportTable( UserDTO userDTO,
                                   @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false, defaultValue = "10")Integer pageSize) {
        return AjaxResult.success(userInfoService.getUserListPage(userDTO, pageNum, pageSize).getResultList());
    }

    /**
     *  修改密码
     */
    @PutMapping("/modifyPwd")
    public AjaxResult modifyPwd(@RequestBody UserModifyPwdDTO userModifyPwdDTO) {
        userInfoService.modifyUserPwd(userModifyPwdDTO);
        return AjaxResult.success(null, "修改密码成功！");
    }

    /**
     *  修改个人信息
     */
    @PutMapping("/modifySelf")
    public AjaxResult modifySelf(@RequestBody UserInfo userInfo) {
        userInfo.setAvatar(userInfo.getImage());
        userInfoService.updateById(userInfo);
        return AjaxResult.success(null, "修改个人信息成功！");
    }

    /**
     *  修改头像
     */
    @PostMapping("/modifyAvatar/{id}")
    public AjaxResult modifyAvatar(@PathVariable Integer id, @RequestBody MultipartFile file) {
        return AjaxResult.success(userInfoService.modifyAvatar(id, file), "上传成功！");
    }

}
